#!/usr/bin/perl
use strict;
use warnings; 
use Bio::Tools::GFF; # GTF= Bio::FeatureIO::gff that initializes with -version => 2.5.
use Data::Dumper;
open (my $fh,"<data/transcripts.gtf") or die $!;
my $gffio = Bio::Tools::GFF->new(-fh => $fh, -version => 2.5);

my ($transcript_length,$id_transcript,$id_gene,$genes,$out,$exons,@transcript);
my $prev_id_gene;
my $i;

while(my $feature = $gffio->next_feature()) {
  $id_gene           = $feature->{_gsf_tag_hash}{gene_id}[0];
  $id_transcript     = $feature->{_gsf_tag_hash}{transcript_id}[0];
  
  if ($feature->primary_tag eq 'exon') {
    push @{$exons->{$id_transcript}},$feature->gff_string;
  }
  
  if ($feature->primary_tag eq 'transcript') {
   
    $transcript_length   = $feature->length;
    $genes->{$id_gene}{$transcript_length} = [$id_transcript,$feature->gff_string];

  }
  last if $i++ ==1000;
}

foreach my $gene (keys %$genes) {
  foreach my $transcript_l (sort {$b<=>$a} keys %{$genes->{$gene}}) {
    my $transc_id  = $genes->{$gene}{$transcript_l}[0];
    #print "$gene\t$transc_id\t$transcript_l\n";
    
    print $genes->{$gene}{$transcript_l}[1]."\n";
    map {print "$_\n" } @{$exons->{$transc_id}};
    last;
  }
}




=comment
my $transcript_length;

while(<$fh>) {
  chomp;
    my @cols   = split;
    my $line   = $_;
    my @subset =  @cols[0,2,3,4,9,11];
    my ($ref,$transcript,$chr_start,$chr_end,$id_gene,$id_transcript) = @subset;
  $id_transcript   =~ s/\"|\;//g;
    $id_gene         =~ s/\"|\;//g;
    
    if ($transcript eq 'transcript') {
            $transcript_length = $chr_end - $chr_start;
            
    }       
        
    } else {
      @longest_orf =();
    }
                                                        
  
}
my $i=0;
for my $id (keys %$data) {
  my @arr = sort {$b <=> $a} @{$data->{$id}};
    #print  $id ."\t@arr\n";
    foreach my $line (@{$data2->{$id."_".$arr[0]}}){
    print "$line\n";    
    }
}
=cut